<?php

namespace App\Utils;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class JwtUtil
{
    private static $key = "uc0jtuG%VRlWmj#9";

    public static function getToken($data)
    {
        $time = time();
        $payload = [
            'iss'  => 'http://cert.zhiwangyunchuang.com',
            'iat'  => $time,
            'nbf'  => $time,
            'exp'  => $time + 7200,
            'data' => $data
        ];
        $token = JWT::encode($payload, self::$key, 'HS256');
        return $token;
    }

    public static function checkToken($token)
    {
        try {
            $decoded = JWT::decode($token, new Key(self::$key, 'HS256'));
        } catch (\Exception $e) {
            if ($e->getMessage() == "Expired token") {
                ApiResponse::fail("token异常，请重新登录", 403);
            }
        }
        return $decoded;
    }
}
